/**
 * lab化合物反应的任务
*/
import { State } from "framework/FSM/AState";
import { LabTaskHelper } from "utils/task/LabTaskHelper";
import { LabUtils } from "./LabUtils";
import { ActionArgHelper } from "GameLoop/Execution/ControlManager/Control/ActionManager/ActionArgHelper";
import { generateUniqueId } from "utils/utils";
 
export class Reaction_State<T extends IStateArg<Room >> extends State<T>
{

    public getActions(arg:T):Action[]
    {
        
        const labentity = arg.entity.getEntity("LabEntity");
        const memory = labentity.getNodeMemory();
        if(!memory.reactionTask  )
        {
            return []
        }
        
        const reactionTask=arg.entity.findTaskById<ITaskDeclare["react"]>(memory.reactionTask
            ,"react");
        if(!reactionTask)
        {
            memory.reactionTask = undefined;
            return []
        }
        const reactionArg =   reactionTask.data.arg;
       
        const coreEntity = arg.entity.getEntity("CoreEntity");
        const reactant = LabTaskHelper.GetReactant(reactionArg);
      
        if(!reactant)
        {
            // 反应栈为空
            return []
        }
        // 初始化和 完成的判断在任务检查时判断
        switch(reactionArg.State)
        {
            case 0:
                {
                    // 清理
                    /**
                     * 清理完成 ->Y 准备
                    */
                    if(LabUtils.isCleanLab(reactant,labentity))
                    {
                        reactionArg.State=1;
                        // 运输完成之后清理运输ID
                        reactionTask.data.transportId=undefined;
                    }
                    else
                    {
                        if(!reactionTask.data.transportId || !arg.entity.findTaskById(reactionTask.data.transportId,"creep"))
                        {
                            
                            const stores = coreEntity.getStores({resourceType:reactionTask.data.arg.RectionTarget,amount:10000});
                            if(stores.length ==0)
                            {
                                return []
                            }
                            // 没有开始运输
                            const baseLabs = labentity.BaseLabs();
                            const reactionLabs = labentity.ReactionLabs();
                            const clearlabs = baseLabs.filter(e=>e.mineralType).concat(reactionLabs.filter(l=>l.mineralType));
                            const transportId = generateUniqueId();
                            
                            
                            reactionTask.data.transportId=transportId;
                            return [ActionArgHelper.actionClearLab(arg.entity,clearlabs,stores[0],transportId)]
                        }
                        else
                        {
                            // 开始运输了
                        }
                        
                        return []
                    }
                    break;
                }
            case 1:
                {
                    // 准备
                    /**
                     *  任务激活 -> Y 清理完成 -> Y 填充
                     *                           N 清理
                     *             N 空闲   
                    */
                    if(LabUtils.isCleanLab(reactant,labentity))
                    {
                        if(!reactionTask.data.transportId || !arg.entity.findTaskById(reactionTask.data.transportId,"creep"))
                        {
                            const baseLabs = labentity.BaseLabs();
                            const leftamount = reactant.Reactant.amount? reactant.Reactant.amount:0;
                            const amount_A = reactant.BaseA.number>leftamount?reactant.BaseA.number-leftamount:0;
                            const labtargets:LabTranportItemArg[]=[]
                            if(amount_A>0)
                            {
                                const storeobjA = coreEntity.getTakeStores ({resourceType:reactant.BaseA.target,amount:amount_A});
                                if(storeobjA.length==0  )
                                {
                                    if(baseLabs[0].mineralType && baseLabs[1].mineralType)
                                    {
                                        reactionArg.State=2;
                                    }
                                    // 资源不足清理反应, 
                                    labentity.log('warn',"警告 State_FillLab-> 仓库资源不足 无法投递任务");
                                    return [];
                                }
                                labtargets.push( {
                                    lab:baseLabs[0],
                                    store:storeobjA[0],
                                    resourceType:reactant.BaseA.target,
                                    amount:amount_A
                                },)
                            }
                            const amount_B = reactant.BaseB.number>leftamount?reactant.BaseB.number-leftamount:0;
                            if(amount_B>0)
                            {
                                const storeobjB = coreEntity.getTakeStores ({resourceType:reactant.BaseB.target,amount:amount_B});
                                if(  storeobjB.length == 0)
                                {
                                    if(baseLabs[0].mineralType && baseLabs[1].mineralType)
                                    {
                                        reactionArg.State=2;
                                    }
                                    // 资源不足清理反应, 
                                    labentity.log('warn',"警告 State_FillLab-> 仓库资源不足 无法投递任务");
                                    return [];
                                }
                                labtargets.push( {
                                    lab:baseLabs[1],
                                    store:storeobjB[0],
                                    resourceType:reactant.BaseB.target,
                                    amount:amount_B
                                },)
                            }
                            
                            if(labtargets.length>0)
                            {
                                reactionTask.data.transportId=generateUniqueId();
                                return [ActionArgHelper.actionFillLab(arg.entity,
                                    labtargets,reactionTask.data.transportId)]
                            }
                            else 
                            {
                                labentity.log('warn',"在准备阶段。但是资源已经就绪");
                            }
                           
                        }
                        else
                        {
                            // 以及开始运输，填充了
                            reactionArg.State=2;
                            // 运输完成之后清理运输ID
                            // reactionTask.data.transportId=undefined;
                        }
                    }
                    else
                    {
                        // 需要清理 转到状态1
                    }
                    break;
                }
            case -1:
                {
                    // 初始化
                    // 报错
                    break;
                }
            case 2:
                {
                    // 填充
                    /**
                     * 底物准备好 -> Y 填充
                    */
                    // arg.entity.log(`warn`,'资源是否就绪',LabUtils.isBaseLabReady(reactant,labentity));
                    if(LabUtils.isBaseLabReady(reactant,labentity))
                    {
                        // 底物准备好了
                        // arg.entity.log(`warn`,'资源已经就绪');
                        reactionTask.data.transportId=undefined;
                        reactionArg.State=3;
                    }
                    break;
                }
            case 3:
                {
                    // 反应
                    /**
                     * 反应结束 -> Y 清理
                    */
                    if(Game.time%REACTION_TIME[reactant.Reactant.target as MineralCompoundConstant] !=0)
                    {
                        return [];
                    }
                    // 错误检测
                    
                    {
                        if(reactant.Reactant.amount && reactant.Reactant.number>reactant.Reactant.amount && reactionTask.data.transportId == undefined )
                        {
                            if(!LabUtils.isReactionReady(reactant,labentity))
                            {
                                reactionArg.State=0;
                            }
                        }
                    }
                    if(reactionTask.data.transportId == undefined)
                    {
                        // 运输完成后反应
                        return [ActionArgHelper.actionLabReaction(arg.entity,reactionTask)];
                    }
                    
                    // break;
                }
            case 4:
                {
                    // 完成
                    break;
                }
        }
        
        // 房间爬虫绑定
        // 返回的Action为一组可以立即执行的Action命令参数
        return [];
    }
}
